package com.pipayshop.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pipayshop.domain.Shop;

import com.pipayshop.domain.ShopCommodity;
import com.pipayshop.domain.vo.EvaluateVO;
import com.pipayshop.domain.vo.PageVo;
import com.pipayshop.domain.vo.ShopCommodityMinVo;
import com.pipayshop.domain.vo.ShopInfoVO;
import com.pipayshop.mapper.ImageMapper;
import com.pipayshop.mapper.OnlineStoreShopCommodityEvaluateMapper;

import com.pipayshop.mapper.OnlineStoreShopCommodityMapper;
import com.pipayshop.mapper.OnlineStoreShopMapper;
import com.pipayshop.service.OnlineStoreShopService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xiaoqiu
 * @date 2024/9/10 11:48
 * @description
 */
@Service
@Slf4j
public class OnlineStoreShopServiceImpl extends ServiceImpl<OnlineStoreShopMapper, Shop> implements OnlineStoreShopService {

    @Autowired
    private OnlineStoreShopMapper shopMapper;
    @Autowired
    private OnlineStoreShopCommodityMapper shopCommodityMapper;
    @Autowired
    private OnlineStoreShopCommodityEvaluateMapper shopCommodityEvaluateMapper;

    @Autowired
    private ImageMapper imageMapper;
        /**
         * 根据评论评分设置网店评分
         * @return
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public Boolean setShopScore() {
            Integer update = shopMapper.setShopScore();
            return update >0;
        }

    @Override
    public Integer getUploadBalance(String shopId) {
        return shopMapper.getUploadBalance(shopId);
    }

    @Override
    public PageVo getShopInfo(String shopId, Integer isDesc, Integer page, Integer limit) {
        page=(page - 1) * limit;
        List<ShopCommodityMinVo> shopCommodityMinVO = shopMapper.getShopCommodityInfo(shopId, isDesc,page, limit);
        LambdaQueryWrapper<ShopCommodity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShopCommodity::getShopId, shopId)
                  .eq(ShopCommodity::getStatus, 0);
        Long size = shopCommodityMapper.selectCount(queryWrapper);
        return new PageVo(size,shopCommodityMinVO);
    }

    @Override
    public PageVo getShopEvaluate(String shopId, Integer page, Integer limit) {
        // 计算 offset
        int offset = (page - 1) * limit;

        // 调用 Mapper 方法获取数据
        List<EvaluateVO> shopCommodityEvaluate = shopCommodityEvaluateMapper.getShopCommodityEvaluate(shopId, offset, limit);

        // 查询总记录数
        Long size = shopCommodityEvaluateMapper.getInfoSize(shopId);

        // 创建并返回 PageVO 对象
        return new PageVo(size, shopCommodityEvaluate);
    }

    /**
     * 根据用户id-网店-升级vip
     *
     * @param userId
     * @return
     */
    @Override
    public boolean upVip(String userId) {
        return shopMapper.update(null, new LambdaUpdateWrapper<Shop>()
                .eq(Shop::getUid, userId)
                .eq(Shop::getStatus, 0)
                .set(Shop::getMembership, true)) > 0;
    }

    /**
     * 根据用户id-判断对应网店是否vip
     */
    @Override
    public boolean isVip(String userId) {
        return shopMapper.selectCount(new LambdaQueryWrapper<Shop>()
                .eq(Shop::getUid, userId)
                .eq(Shop::getStatus, 0)
                .eq(Shop::getMembership, true)) > 0;
    }

    /**
     * 根据用户id查询 对应的 网店关注列表
     *
     * @param userId
     * @return
     */
    @Override
    public PageVo getFollowList(String userId, Integer page, Integer limit) {
        Long integer = shopMapper.selectAllFollowShopByUserId(userId);
        List<ShopInfoVO> shopInfos = shopMapper.selectFollowShopByUserId(userId, (page - 1) * limit, limit);
        return new PageVo(integer,shopInfos);
    }

    /**
     * 根据用户id获取网店信息
     *
     * @param userId
     * @return
     */
    @Override
    public List<ShopInfoVO> getShopInfoByUid(String userId) {
        List<ShopInfoVO> shopInfoVOS = shopMapper.selectShopInfoByshopIdOrUserId(userId, null);
        for (ShopInfoVO shopInfoVO : shopInfoVOS) {
            String shopImagList = shopInfoVO.getShopImagList();
            List<String> imageList = new ArrayList<>();

            if (shopImagList != null && !shopImagList.isEmpty()) {
                imageList = JSON.parseArray(shopImagList, String.class)
                        .stream()
                        .parallel()
                        .map(imageId -> imageMapper.selectPath(imageId))
                        .collect(Collectors.toList());
            }

            shopInfoVO.setShopImagList(imageList.toString());
        }

        return shopInfoVOS;
    }

    @Override
    public PageVo getAllShopInfo(String shopId, int isDesc, int page, int limit) {
        page=(page - 1) * limit;

        List<ShopCommodityMinVo> shopCommodityMinVO = shopMapper.getAllShopCommodityInfo(shopId, isDesc,page, limit);
        LambdaQueryWrapper<ShopCommodity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShopCommodity::getShopId, shopId)
        ;
        Long size = shopCommodityMapper.selectCount(queryWrapper);
        return new PageVo(size,shopCommodityMinVO);
    }

    @Override
    public List<ShopInfoVO> getShopInfoByShopid(String shopId) {
        List<ShopInfoVO> shopInfoVOS = shopMapper.selectShopInfoByshopIdOrUserId(null, shopId);
        for (ShopInfoVO shopInfoVO : shopInfoVOS) {
            String shopImagList = shopInfoVO.getShopImagList();
            List<String> imageList = new ArrayList<>();

            if (shopImagList != null && !shopImagList.isEmpty()) {
                imageList = JSON.parseArray(shopImagList, String.class)
                        .stream()
                        .parallel()
                        .map(imageId -> imageMapper.selectPath(imageId))
                        .collect(Collectors.toList());
            }

            shopInfoVO.setShopImagList(imageList.toString());
        }

        return shopInfoVOS;
    }


}
